package cn.shadow.module.snake;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

/**
 * 点击 监听器（只包括 手指点击 或 鼠标点击）
 */
public class MyClickListener extends ClickListener {

    /**
     * 对象（演员/舞台）被点击时调用
     *
     * @param x
     * 		点击时（手指抬起时）的 X 轴坐标, 相对于被点击对象（监听器注册者）的左下角
     *
     * @param y
     * 		点击时（手指抬起时）的 Y 轴坐标, 相对于被点击对象（监听器注册者）的左下角
     */
    @Override
    public void clicked(InputEvent event, float x, float y) {
        // 获取响应这个点击事件的演员
        Actor actor = event.getListenerActor();
        System.out.println("被点击: " + x + ", " + y + "; Actor: " + actor.getClass().getSimpleName());
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointer) {
        // 获取响应这个点击事件的演员
        Actor actor = event.getListenerActor();
        actor.setPosition(x,y);
    }

    @Override
    public boolean mouseMoved(InputEvent event, float x, float y) {

        Actor listenerActor = event.getListenerActor();
        Group group = (Group) listenerActor;
        Actor head = group.getChildren().first();
        if (head instanceof SnakeNode) {
            float x1 = head.getX();
            float y1 = head.getY();
            float angle = calAngle(x1, y1, x, y);
            ((SnakeNode) head).setDegree(angle);
        }
        return true;
    }

    public static float calAngle(double x1, double y1, double x2, double y2) {
        double x = x2 - x1;
        double y = y2 - y1;
        double z = Math.sqrt(x * x + y * y);
        return (float) (Math.asin(y / z) / Math.PI * 180);
    }


    public static void main(String[] args) {
        float x1=100;
        float y1=100;
        float x2=-200;
        float y2=-200;
        float angle = calAngle(x1, y1, x2, y2);
        double v = Math.atan2((x2 - x1), (y2 - y1));
        System.out.println(v/ Math.PI * 180);
        System.out.println(angle);
    }
}
